package com.neurotech.pcc.ui


import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.NavHostFragment
import com.neurotech.pcc.R
import com.neurotech.pcc.dummy.RemoteModel
import com.neurotech.pcc.dummy.RemotesDatabase
import com.neurotech.pcc.other.darkStatusBarIcon
import com.neurotech.pcc.other.pop
import com.neurotech.pcc.other.toastCenter
import com.neurotech.pcc.vm.RemotesRepository
import com.neurotech.pcc.vm.RemotesRepositoryFactory
import com.neurotech.pcc.vm.RemotesViewModel
import com.neurotech.pcc.widgets.IpInputLayout
import com.neurotech.pcc.widgets.MacInputLayout
import kotlinx.android.synthetic.main.fragment_config.*
import kotlin.concurrent.thread

/**
 * A Config Fragment class.
 * used add remote device
 * @author shenhua
 */
class ConfigFragment : Fragment() {

    private var isEdit = false
    private lateinit var remotesViewModel: RemotesViewModel

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val factory = RemotesRepositoryFactory(RemotesRepository.dao(RemotesDatabase.getInstance(context
                ?: activity!!).remoteDao()))
        remotesViewModel = ViewModelProviders.of(this, factory)[RemotesViewModel::class.java]
        return inflater.inflate(R.layout.fragment_config, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (activity!! as MainActivity).setSupportActionBar(toolbar)
        toolbar.setNavigationOnClickListener { pop() }
        (activity!! as MainActivity).supportActionBar?.setDisplayHomeAsUpEnabled(true)

        isEdit = arguments?.getBoolean("isEdit", false)!!
        val remoteModel = arguments?.getSerializable("remoteModel") as RemoteModel?

        ipLayout.fitIp(remoteModel?.host)
        portLayout.fitPort(remoteModel?.port)
        macLayout.fitMac(remoteModel?.mac)
        etAlias.setText(remoteModel?.alias)

        listener(remoteModel)
    }

    private fun listener(remoteModel: RemoteModel?) {
        confirm.setOnClickListener {
            val ip = ipLayout.ip()
            val port = portLayout.port()
            val mac = macLayout.mac()
            val alias = etAlias.text.toString()

            if (!IpInputLayout.isIp(ip)) {
                toastCenter(getString(R.string.ip_error))
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(port)) {
                toastCenter(getString(R.string.port_error))
                return@setOnClickListener
            }
            if (!MacInputLayout.isMac(mac)) {
                toastCenter(getString(R.string.mac_error))
                return@setOnClickListener
            }
            thread {
                if (isEdit) {
                    if (remoteModel != null) {
                        remoteModel.host = ip
                        remoteModel.port = port
                        remoteModel.mac = mac
                        remoteModel.alias = alias
                        remotesViewModel.update(remoteModel)
                    }
                } else {
                    remotesViewModel.insert(RemoteModel(ip, port, alias, mac))
                }
            }

            NavHostFragment.findNavController(this).popBackStack(R.id.mainFragment, false)
        }
    }

    override fun onResume() {
        super.onResume()
        darkStatusBarIcon(activity?.window?.decorView)
    }

}
